<html>
<head>
    <title>{{$klg->title}}</title>
    <link rel="stylesheet" href="{{env('APP_ROOT')}}/css/showpage.css">
    <script src="{{env('APP_ROOT')}}/assets/jquery/jquery.js"></script>
    <script src="{{env('APP_ROOT')}}/assets/layui/layui.all.js"></script>
</head>
<body>
<div id="news_main">
    <div id="news_title"><a href="#">{{$klg->title}}</a></div>
    <div id="news_info">
        <span class="news_poster">作者 {{$klg->publisher->name}}</span>
        <span class="time">发布于 {{$klg->created_at}}</span>
        <span class="comment">
                <span>{{$klg->categories}}</span>
            </span>
        <span class="view" id="News_TotalView">有{{$klg->views}}人阅读</span>

    </div>
    <div id="news_content">
        <div id="news_body">
        {!! $klg->content !!}
        </div>
        <div id="news_otherinfo"></div>
    </div>
    @if($errors->any())
        <div style="color: red;display:block;text-align:center;">
            <ul class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <span id="Comment_new"></span>
    <div id="comment_main">
        <div id="comment_main_list">
            @foreach($klg->msg as $msg)
            <div class="user_comment">
                <div class="commenter_info">
                    <span class="comment-author">{{$msg->user_name}}({{$msg->user_email}})</span><span
                            class="time">发表于 {{$msg->created_at}} </span>
                    @needspermissions('knowledge-settop',false)
                    @if(!$msg->top)
                    <a href="{{route('knowledge.settop')}}?id={{$msg->id}}&kid={{$msg->klg_id}}" class="graylink">置顶</a>
                        @endif
                    @endauth
                    &nbsp;</div>
                <div class="comment_main">{{$msg->msg}}</div>

                <div class="clear"></div>
            </div>
                @endforeach
        </div>
    </div>
    <span id="klgId" style="display: none;">{{$klg->id}}</span>
    <div id="comment_form_block" class="qitem_reply"><textarea name="txtContent" id="txtContent"
                                                               onkeydown="ctlent2(event)"></textarea><br><input
                type="button" id="btnComment" value="提交评论" onclick="doComment();"> Ctrl + Enter键快速提交
    </div>
</div>
<script>
    function ctlent2(eventobject) {
        if (eventobject.ctrlKey && eventobject.keyCode == 13) {
            doComment();
        }
    }
    function doComment() {
        var Content = $.trim($("#txtContent").val());
        Content = Content.replace(/(\n){3,}/ig, '\n\n');
        var klgId = $("#klgId").html();
        if (Content == "") {
            layer.msg("评论不能为空！");
            return;
        } else if (Content.length < 3) {
            layer.msg('回复的字数太少了，至少3个字吧！');
            return;
        }
        $("#btnComment").val("提交中...").attr("disabled", "disabled");
        $("#Comment_new").html("评论提交中...").css({'color':'red','display':'block','text-align':'center'});

        var insertComment = {};
        insertComment.msg = Content;
        insertComment.klg_id = klgId;
        $.ajax({
            timeout: 30000, //30s
            url: '{{route("knowledge.addcomment")}}',
            data: JSON.stringify(insertComment),
            type: 'post',
            dataType: 'text',
            headers:{'X-CSRF-TOKEN' : "{{ csrf_token() }}"},
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                window.location.reload()
            },
            error: function (xhr, textStatus) {
                var data;
                if (xhr.status == 500) {
                    data = "抱歉！发生了错误！";
                } else if (xhr.status > 0) {
                    data = "抱歉！评论提交失败！错误信息：" + xhr.responseText;
                } else {
                    data = "抱歉！评论提交失败！出错原因：" + textStatus;
                }
                $("#btnComment").removeAttr("disabled");
                $("#Comment_new").css({'color':'red','display':'block','text-align':'center'}).html(data);
            }
        });

    }
</script>
</body>
</html>
